package jia.core;

import jia.core.graphics.WallImage;
import jia.core.graphics.WallImageManager;
import jia.core.flags.WallFlags;

import java.awt.image.BufferedImage;

/**
 * Object that defines all aspects of a Wall, including what direction the wall faces, and the
 * image to use when rendering it.
 * @author Alec Lanter
 */
public class Wall {
	protected long id;
	private String wallImageName;
	private String wallImageParserName;
	private WallImage image;
	private WallFlags flags;
	private Compass facing = Compass.NORTH;
	private Space parentSpace = null;

	// ====================================================
	/**
	 */
	public Wall() {
		flags = new WallFlags();
	}

	// ===============================================
	/**
	 * @param wallImageName
	 * @param wallImageParser
	 * @param facing
	 */
	public Wall(String wallImageName,
				String wallImageParser,
				Compass facing) {
		this();
		this.wallImageName = wallImageName;
		this.wallImageParserName = wallImageParser;
		this.facing = facing;
		checkLoadImage();
	}

	// ===============================================
	/**
	 * @param wallImageName
	 * @param wallImageParser
	 * @param facing
	 * @param flags
	 */
	public Wall(String wallImageName,
				String wallImageParser,
				Compass facing,
				WallFlags flags) {
		this(wallImageName, wallImageParser, facing);
		this.flags = new WallFlags(flags.getIntVal());
	}

	// ====================================================
	/**
	 * @param wallImageName The Wall Image to set
	 */
	public void setImageName(String wallImageName) {
		this.wallImageName = wallImageName;
		checkLoadImage();
	}

	// ====================================================
	/**
	 */
	private void checkLoadImage() {
		if (wallImageName != null && wallImageParserName != null)
			image = WallImageManager.getWallImage(wallImageName, wallImageParserName);
	}

	// ====================================================
	/**
	 * @return
	 */
	public String getImageName() {
		return wallImageName;
	}

	// ====================================================
	/**
	 * @return
	 */
	public WallImage getImage() {
		return image;
	}

	// ====================================================
	/**
	 * @return Returns the flags.
	 */
	public WallFlags getFlags() {
		return flags;
	}

	// ===============================================
	/**
	 * @return
	 */
	public Compass getFacing() {
		return facing;
	}

	// ===============================================
	/**
	 * @param facing
	 */
	public void setFacing(Compass facing) {
		this.facing = facing;
	}

	// ====================================================
	/**
	 * @param facingValue
	 */
	public void setFacingValue(int facingValue) {
		this.facing = Compass.forDirectionalValue(facingValue);
	}

	// ====================================================
	/**
	 * @return
	 */
	public int getFacingValue() {
		return facing.getDirectionalValue();
	}

	// ===============================================
	/**
	 * @return
	 */
	public Space getParentSpace() {
		return parentSpace;
	}

	// ===============================================
	/**
	 * @param parentSpace
	 */
	public void setParentSpace(Space parentSpace) {
		this.parentSpace = parentSpace;
	}

	// ===============================================
	/**
	 * @param viewportFacing
	 * @param distanceLevel
	 * @return
	 */
	public BufferedImage getTexture(Compass viewportFacing, int distanceLevel) {
		if (distanceLevel > image.getMaxDistanceLevel()) {
			distanceLevel = image.getMaxDistanceLevel();
		}
		return image.getTexture(facing, viewportFacing, distanceLevel);
	}

	// ====================================================
	/**
	 * @return Returns the wallImageParserName.
	 */
	public String getWallImageParserName() {
		return wallImageParserName;
	}

	// ====================================================
	/**
	 * @param wallImageParserName The wallImageParserName to set.
	 */
	public void setWallImageParserName(String wallImageParserName) {
		this.wallImageParserName = wallImageParserName;
		checkLoadImage();
	}

	// ===============================================
	/**
	 * @return Returns the wallImageName.
	 */
	public String getWallImageName() {
		return wallImageName;
	}

	// ===============================================
	/**
	 * @param wallImageName The wallImageName to set.
	 */
	public void setWallImageName(String wallImageName) {
		this.wallImageName = wallImageName;
	}

	// ===============================================
	/**
	 * @param flags The flags to set.
	 */
	public void setFlags(WallFlags flags) {
		this.flags = flags;
	}
}
